package com.cdp.web.controller.system;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysWaterReducer;
import com.cdp.system.dto.WaterReducerDTO;
import com.cdp.system.service.ISysWaterReducerService;
import com.cdp.system.vo.AdditiveWaterReducerVO;
import com.cdp.system.vo.IdsVO;
import com.cdp.system.vo.SandFineneVO;
import com.cdp.system.vo.WaterReducerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/system/waterReducer")
public class SysWaterReducerController {


    @Autowired
    private ISysWaterReducerService sysWaterReducerService;


    @GetMapping("listWaterReduce")
    private AjaxResult listWaterReduce() {
        List<SysWaterReducer> waterReducerList = sysWaterReducerService.listWaterReduce();
        return AjaxResult.success(waterReducerList);
    }


    /**
     * 原材料
     * 减水剂模糊查询
     * @param waterReducerDTO
     * @return
     */
    @PostMapping("listMistedWaterReduce")
    public AjaxResult listMistedWaterReduce(@RequestBody WaterReducerDTO waterReducerDTO) {
        // 参数校验：确保 solidContent 和 reducingRate 的长度为 0 或 2
        if (waterReducerDTO.getSolidContent() != null && waterReducerDTO.getSolidContent().size() != 0 && waterReducerDTO.getSolidContent().size() != 2) {
            log.error("参数 solidContent 长度不为 0 或 2: {}", waterReducerDTO.getSolidContent());
            return AjaxResult.error("solidContent 参数无效，长度必须为 0 或 2。");
        }

        if (waterReducerDTO.getReducingRate() != null && waterReducerDTO.getReducingRate().size() != 0
                && waterReducerDTO.getReducingRate().size() != 2) {
            log.error("参数 reducingRate 长度不为 0 或 2: {}", waterReducerDTO.getReducingRate());
            return AjaxResult.error("reducingRate 参数无效，长度必须为 0 或 2。");
        }

        // 执行业务逻辑，调用服务层获取数据
        try {
            List<WaterReducerVO> waterReducerVOList = sysWaterReducerService.listMistedWaterReduce(waterReducerDTO);
            return AjaxResult.success(waterReducerVOList);
        } catch (Exception e) {
            log.error("查询执行时发生错误，参数为: {}", waterReducerDTO, e);
            return AjaxResult.error("系统内部错误，请稍后重试。");
        }
    }


    /**
     * 外加剂
     * 减水剂模糊查询
     * @return
     */
    @PostMapping("listAdditiveMistedWaterReduce")
    public  AjaxResult listAdditiveMistedWaterReduce(@RequestBody WaterReducerDTO waterReducerDTO){


        // 执行业务逻辑，调用服务层获取数据
        try {
            List<AdditiveWaterReducerVO> waterReducerVOList = sysWaterReducerService.listAdditiveMistedWaterReduce(waterReducerDTO);
            return AjaxResult.success(waterReducerVOList);
        } catch (Exception e) {
            log.error("查询执行时发生错误，参数为: {}", waterReducerDTO, e);
            return AjaxResult.error("系统内部错误，请稍后重试。");
        }

    }


    /**
     * 减水剂id模糊查询
     * @param id
     * @return
     */

    @GetMapping("/queryWaterReducerIdsById")
    public AjaxResult queryWaterReducerIdsById(@RequestParam(required = false) Integer id) {

        System.out.println("我接收的id"+id);
        List<IdsVO> idsVOS = sysWaterReducerService.queryWaterReducerIdsById(id);
        // 将 idsVOS 中的 ids 列表转换为所需格式
        List<SandFineneVO> sandFinenessVOList = idsVOS.stream()
                .flatMap(idsVO -> idsVO.getIds().stream()) // 提取 ids 列表
                .map(idValue -> {
                    SandFineneVO vo = new SandFineneVO();
                    vo.setValue(String.valueOf(idValue)); // 将 Integer 转为 String
                    vo.setLabel(String.valueOf(idValue));
                    return vo;
                })
                .collect(Collectors.toList());

        return AjaxResult.success(sandFinenessVOList);

    }




}
